﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class DataConvert
    {
        /// <summary>
        /// 空字符串
        /// </summary>
        public const string DefaultStringValue = "";
        /// <summary>
        ///默认整型值 0
        /// </summary>
        public const int DefaultIntValue = 0;
        
        /// <summary>
        /// 默认时间值
        /// </summary>
        public static DateTime DefaultDateTimeValue = DateTime.Parse("2015-01-01 00:00:00");
        public static float DefaultFloatValue = float.Parse("0");
        public const int DefaultDoubleValue = 0;
        public static bool DefaultBoolValue = true;
        /// <summary>
        /// 转换成int型
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static int ToInt(object v)
        {
            if (IsNullOrDBNull(v))
            {
                return DefaultIntValue;
            }
            else
            {
                try
                {
                    return int.Parse(v.ToString());
                }
                catch
                {
                    return DefaultIntValue;
                }

            }
        }
        /// <summary>
        /// 转换成bool型
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool ToBool(object v)
        {
            if (IsNullOrDBNull(v))
            {
                return DefaultBoolValue;
            }
            else
            {
                try
                {
                    return bool.Parse(v.ToString());
                }
                catch
                {
                    return DefaultBoolValue;
                }

            }
        }
        /// <summary>
        /// 转换成float型
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static float ToFloat(object v)
        {
            if (IsNullOrDBNull(v))
            {
                return DefaultFloatValue;
            }
            else
            {
                try
                {
                    return float.Parse(v.ToString());
                }
                catch
                {
                    return DefaultFloatValue;
                }

            }
        }
        /// <summary>
        /// 转换成double型
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static double ToDouble(object v)
        {
            if (IsNullOrDBNull(v))
            {
                return DefaultDoubleValue;
            }
            else
            {
                try
                {
                    return double.Parse(v.ToString());
                }
                catch
                {
                    return DefaultDoubleValue;
                }

            }
        }
        /// <summary>
        /// 转换成时间
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(object v)
        {
            if (IsNullOrDBNull(v))
            {
                return DefaultDateTimeValue;
            }
            else
            {
                try
                {
                    return DateTime.Parse(v.ToString());
                }
                catch (Exception)
                {
                    return DefaultDateTimeValue;
                }
            }
        }
        /// <summary>
        /// 转换成string
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static string ToString(object v)
        {
            if (IsNullOrDBNull(v))
            {
                return DefaultStringValue;
            }
            else
            {
                try
                {
                    if (v.ToString() == "0001-01-01 00:00:00" || v.ToString() == "2015-01-01 00:00:00" || v.ToString() == "2015-01-01" || v.ToString() == "0001-01-01")
                    {
                        v = "";
                    }
                    return v+"";
                }
                catch (Exception)
                {
                    return DefaultStringValue;
                }
            }
        }

        /// <summary>
        /// 判断对象是否为null 或dbnull 
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static bool IsNullOrDBNull(object v)
        {
            if (v == null || Convert.IsDBNull(v) || string.IsNullOrEmpty(Convert.ToString(v)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 数据类型
        /// </summary>
        public enum DataType
        {
            IntType,
            LongType,
            DoubleType
        }

        public static int ToInt2(object v)
        {
            int page = ToInt(v);
            if (page == 0)
            {
                return 1;
            }
            else
            {
                return page;
            }
        }
    }
}
